package com.example.myserver;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity3 extends AppCompatActivity {
    private List<String> status = new ArrayList<>();

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        requestPermissions(new String[]{Manifest.permission.ACCESS_BACKGROUND_LOCATION, Manifest.permission.READ_PHONE_STATE}, 0X123);
        Log.i("TAGFF", "onCreate: ");
    }
    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        Log.i("TAGFF", "onRequestPermissionsResult: ");
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 0x123 ) {
//            获取系统的TelephoneManager对象
            Log.i("TAGFF", "onRequestPermissionsResult: ");
            TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
//            获取代表状态名称的数组
            String[] starusNames = getResources().getStringArray(R.array.statusName);
            String[] simState = getResources().getStringArray(R.array.simStatus);
            String[] phoneType = getResources().getStringArray(R.array.phoneType);
            status.add(telephonyManager.getImei());
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            status.add(telephonyManager.getDeviceSoftwareVersion() != null ? telephonyManager.getDeviceSoftwareVersion() : "未知");
//            获取网络运营商代号
            status.add(telephonyManager.getNetworkOperator());
            Log.i("TAGFF", telephonyManager.getNetworkOperator());
//            获取网络运营商名称
            status.add(telephonyManager.getNetworkOperatorName());
            Log.i("TAGFF",telephonyManager.getNetworkOperator() );
//            获取手机网络类型
            status.add(phoneType[telephonyManager.getPhoneType()]);
//            获取设备的蜂窝状态
//            status.add(telephonyManager.getAllCellInfo() != null ? telephonyManager.getAllCellInfo().toString() : "未知");
//            获取sim卡的国别
            status.add(telephonyManager.getSimCountryIso());
//            获取sim卡序列号
            status.add(telephonyManager.getNetworkOperatorName());
//            获取sim卡状态
            status.add(simState[telephonyManager.getSimState()]);
            for (int i = 0; i < status.size(); i++) {
                System.out.println(status.get(i));
            }
            ListView listView = findViewById(R.id.ls);
            List<Map<String, String>> list = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                Map<String, String> map = new HashMap<>();
                map.put("name", starusNames[i]);
                map.put("valus", status.get(i));
                list.add(map);
            }

            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i).get("name"));
            }
            SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.line, new String[]{"name", "value"}, new int[]{R.id.txt, R.id.txt});
            listView.setAdapter(adapter);
        } else {
            Toast.makeText(this, R.string.app_name, Toast.LENGTH_SHORT).show();
        }
    }
}